﻿using Microsoft.AspNetCore.Mvc;
using Novel.Web.Models;
using Novel.Web.Models.Entity;
using System.Text.RegularExpressions;
using XjjXmm.FrameWork.ToolKit;
using XjjXmm.Infrastructure.Repository;
using XjjXmm.Infrastructure.Repository.Model;
using XjjXmm.Spider.Entity;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Novel.Web.Controllers
{
    [Route("api/novel")]
    [ApiController]
    public class NovelApiController : ControllerBase
    {
        private readonly NovelService novelService;
        public NovelApiController( NovelService novelService)
        {
            this.novelService = novelService;
        }
        // GET: api/<NovelApiController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<NovelApiController>/5
        [HttpGet("{type}/{category}/{id}")]
        public async Task<PageOutput<BookEntity>> Get(string type, string category, int page = 1)
        {
            var currentMenu = EnumKit.GetEnums<MenuEnum>().FirstOrDefault(t => t.ToString().ToLower() == category.ToLower());

            return await novelService.GetNovelByCategory(new PageInput<string>
            {
                CurrentPage = page,
                Filter = currentMenu.ToDescription()
            });
          
        }

        [HttpGet("top")]
        public async Task<List<BookEntity>> GetTop()
        {

            return await novelService.GetTop(null, 10);
        }

        [HttpGet("hot")]
        public async Task<List<BookEntity>> GetHot()
        {
            return await novelService.GetHot(null, 10);
        }

        [HttpGet("latest")]
        public async Task<List<BookEntity>> GetLatest()
        {
            var res = await novelService.GetNovelByCategory(new PageInput<string>
            {
                CurrentPage = 1,
                PageSize = 10
            });

            return res.Data;
        }

        [HttpGet]
        [Route("chapter/{nid}")]
        public async Task<List<BookChapterEntity>> ChapterList(long nid)
        {
            return await novelService.GetChaptersByNovelId(nid);
        }

        [HttpGet]
        [Route("content/{nid}/{cid}")]
        public async Task<IEnumerable<string>> Content(long nid, long cid)
        {
            var contents = await novelService.GetChapterContent(nid, cid);

          /*  var contents = Regex.Replace(current, "<br>", "").Replace("&nbsp;", "").Split('\n')
                .Where(t=>t!="");*/
            return contents;
        }

        // POST api/<NovelApiController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<NovelApiController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<NovelApiController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
